package learn_demo.genshuixue;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
 * TODO 跟谁学面试记录，遍历HashMap~
 *
 * @author david
 * @version 1.0
 * @date 2021/4/20 14:56
 */
public class HashMapDemo {
    public static void main(String[] args) {
        hash();
    }

    public static void hash() {
        HashMap<String, Integer> map = new HashMap<>();
        map.put("a", 1);
        map.put("b", 1);
        map.put("c", 1);
        map.put("d", 1);
        map.put("e", 1);
        //最简洁高效的方式 lambda
        System.out.println("lambda方式，最简洁高效");
        map.forEach((k, v) -> System.out.println("ket:" + k + ",value:" + v));

        //常用高效
        System.out.println("常用高效");
        for (Map.Entry<String, Integer> entry : map.entrySet()) {
            System.out.println(entry.getKey());
        }

        //分开使用
        System.out.println("分开使用");
        for (String k : map.keySet()) {
            System.out.println(k);
        }
        for (Integer integer : map.values()) {
            System.out.println(integer);
        }

        //方法4 entrySet Iterator
        System.out.println("entrySet Iterator");
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> entry = iterator.next();
            System.out.println(entry.getKey() + "|" + entry.getValue());
        }

        //方法5 最低效
        System.out.println("方法5，最低效");
        for (String e : map.keySet()) {
            System.out.println(e + "|" + map.get(e));
        }

    }
}

